package org.sdnstproject.interlayer.OF13Protocal;

import java.util.HashMap;
import org.projectfloodlight.openflow.types.U32;
import org.projectfloodlight.openflow.types.U8;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

public class FuFeaturesReply {

	private static final Logger log = LoggerFactory.getLogger(FuFeaturesReply.class);
    public byte version = 4;
    public byte type = 0x6;
    public short length = 32;
    public long xid = 0x0L;//需要dummyswitch设置
    public long datapathId = (long) 1;//需要dummyswitch设置
    public long nBuffers = (long) 256;
    public short nTables = (short) 254;
    public short auxiliaryId = (short) 0;
    /*public enum OFCapabilities {
	public final static int FLOW_STATS_VAL = 0x1;
    public final static int TABLE_STATS_VAL = 0x2;
    public final static int PORT_STATS_VAL = 0x4;
    public final static int IP_REASM_VAL = 0x20;
    public final static int QUEUE_STATS_VAL = 0x40;
    public final static int GROUP_STATS_VAL = 0x8;
    public final static int PORT_BLOCKED_VAL = 0x100;
     * */
    public int capabilities = 1;//FLOW_STATS
    public long reserved = (long) 0;
    
    public FuFeaturesReply() {
    	
    }
    
    
    public FuFeaturesReply setField(String field, String value) {
		switch (field) {
		case "version":
			this.version = Byte.parseByte(value);
			break;
		case "type":
			this.type = Byte.parseByte(value);
			break;
		case "length":
			this.length = Short.parseShort(value);
			break;
		case "xid":
			this.xid = Long.parseLong(value);
			break;
		case "datapathId":
			this.datapathId = Long.parseLong(value);
			break;
		case "nBuffers":
			this.nBuffers = Long.parseLong(value);
			break;
		case "nTables":
			this.nTables = Short.parseShort(value);
			break;
		case "auxiliaryId":
			this.auxiliaryId = Short.parseShort(value);
			break;
		case "capabilities":
			this.capabilities = Integer.parseInt(value);
			break;
		case "reserved":
			this.reserved = Long.parseLong(value);
			break;
		}
		return this;
	}
    
    public ByteBuf writeTo() {
    	ByteBuf bb = Unpooled.directBuffer();
        bb.writeByte(version);
        bb.writeByte(type);
        bb.writeShort(length);
        bb.writeInt(U32.t(xid));
        bb.writeLong(datapathId);
        bb.writeInt(U32.t(nBuffers));
        bb.writeByte(U8.t(nTables));
        bb.writeShort(auxiliaryId);
        // pad: 2 bytes
        bb.writeZero(2);
        bb.writeInt(capabilities);
        bb.writeInt(U32.t(reserved));
        return bb;
    }
    
    /**记录FuFeaturesReply字段和其类型映射*/
    public HashMap<String, String> FuFeaturesReplyFields = new HashMap<String, String>() {
		{
			put("version","byte");
			put("type","byte");
			put("length","short");
			put("xid","long");
			put("datapathId", "long");
			put("nBuffers", "long");
			put("nTables", "short");
			put("auxiliaryId", "short");
			put("capabilities", "int");
			put("reserved", "long");
		}
    };
    
    @Override
    public String toString() {
        StringBuilder b = new StringBuilder("OFFeaturesReplyVer13(");
        b.append("version=").append(version);
        b.append(", ");
        b.append("type=").append(type);
        b.append(", ");
        b.append("length=").append(length);
        b.append(", ");
        b.append("xid=").append(xid);
        b.append(", ");
        b.append("datapathId=").append(datapathId);
        b.append(", ");
        b.append("nBuffers=").append(nBuffers);
        b.append(", ");
        b.append("nTables=").append(nTables);
        b.append(", ");
        b.append("auxiliaryId=").append(auxiliaryId);
        b.append(", ");
        b.append("capabilities=").append(capabilities);
        b.append(", ");
        b.append("reserved=").append(reserved);
        b.append(")");
        return b.toString();
    }
    
    
}
